package com.findme.test.itext.pdf;

import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;

/**
 * @ClassName: CellFactory
 * @description:
 * @author: find me
 * @create: 2022-04-13 13:50
 * @Version 1.0
 */
public class CellFactory {

    // 字体
    private Font font;
    //  水平样式 0-left, 1-center, 2-right
    private int horizontalAlignment;
    // 垂直样式  4-top, 5-middle, 6-bottom;
    private int verticalAlignment;
    // 列合并
    private int colspan;
    // 行合并
    private int rowspan;
    /**
     * 外边框
     * 0-默认
     * 1-隐藏上边框
     * 2-隐藏下边框
     * 3-隐藏上、下边框
     * 4-隐藏左边框
     * 5-隐藏左、上边框
     * 6-隐藏左、下边框
     * 7-隐藏左、上、下边框
     * 8-隐藏右边框
     * 9-隐藏右、上边框
     * 10-隐藏右、下边框
     * 11-隐藏右、上、下边框
     * 12-隐藏左、右边框
     * 13-隐藏上、左、右边框
     * 14-隐藏下、左、右边框
     * 15-隐藏全部
     */
    private int borderSide;

    // 最小高度
    private float minimumHeight;

    /**
     * @param value               文本
     * @param font                字体
     * @param horizontalAlignment 水平样式 0-left, 1-center, 2-right
     * @param verticalAlignment   垂直样式  4-top, 5-middle, 6-bottom;
     * @param colspan             列合并
     * @param rowspan             行合并
     * @param borderSide          外边框
     *                            0-默认
     *                            1-隐藏上边框
     *                            2-隐藏下边框
     *                            3-隐藏上、下边框
     *                            4-隐藏左边框
     *                            5-隐藏左、上边框
     *                            6-隐藏左、下边框
     *                            7-隐藏左、上、下边框
     *                            8-隐藏右边框
     *                            9-隐藏右、上边框
     *                            10-隐藏右、下边框
     *                            11-隐藏右、上、下边框
     *                            12-隐藏左、右边框
     *                            13-隐藏上、左、右边框
     *                            14-隐藏下、左、右边框
     *                            15-隐藏全部
     * @return
     */
    public PdfPCell setCelText(PdfPCell cell, String value) {
        cell.setPhrase(new Phrase(value, font));//存值
        return cell;
    }

    public CellFactory(Font font, int horizontalAlignment, int verticalAlignment, int colspan, int rowspan, int borderSide, float minimumHeight) {
        this.font = font;
        this.horizontalAlignment = horizontalAlignment;
        this.verticalAlignment = verticalAlignment;
        this.colspan = colspan;
        this.rowspan = rowspan;
        this.borderSide = borderSide;
        this.minimumHeight = minimumHeight;
    }

    public CellFactory() {
    }

    public PdfPCell setCellStyle(PdfPCell cell) {

        cell.setHorizontalAlignment(this.horizontalAlignment);
        cell.setMinimumHeight(this.minimumHeight);
        if (this.verticalAlignment > 0) {
            cell.setUseAscender(true);//垂直居中
        }
        cell.setVerticalAlignment(this.verticalAlignment);
        if (this.colspan > 0) {
            cell.setColspan(this.colspan);
        }
        if (this.rowspan > 0) {
            cell.setRowspan(this.rowspan);
        }
        if (this.borderSide > 0) {
            cell.disableBorderSide(this.borderSide);
        }

        return cell;
    }

    public PdfPCell generatePdfCell(String value) {
        PdfPCell cell = setCellStyle(new PdfPCell());
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public PdfPCell generatePdfCell(String value, float followingIndent) {
        PdfPCell cell = generatePdfCell(value);
        cell.setFollowingIndent(followingIndent);
        return cell;
    }

    public PdfPCell generatePdfCell() {
        PdfPCell cell = setCellStyle(new PdfPCell());
        return cell;
    }

    public PdfPCell generatePdfCell(float followingIndent) {
        PdfPCell cell = setCellStyle(new PdfPCell());
        cell.setFollowingIndent(followingIndent);
        return cell;
    }
}
